﻿Shader "Dan/UI/UI_MultiFX01"
{
    Properties
    {
        _Color ("Color", Color) = (0.07843138,0.3921569,0.7843137,1)
        _LoadingColor ("LoadingColor", Color) = (1,0,0,1)
        _Emission ("Emission", Float ) = 1
        _FadeIn ("FadeIn", Range(0, 1)) = 0
        _FadeOut ("FadeOut", Range(0, 1)) = 0
        _MainTex_B ("MainTex_B", 2D) = "white" {}
        _RotateSpeed_R ("RotateSpeed_R", Float ) = 0.5
        _RotateSpeed_G ("RotateSpeed_G", Float ) = -1
        _RotateSpeed_B ("RotateSpeed_B", Float ) = 0.5
        _ShinnySpeed ("ShinnySpeed", Float ) = 1
        _MainTexB_Bdepth ("MainTexB_B.depth", Float ) = 0
        _DepthOffsetSpeed ("OffsetSpeed_B", Float ) = 0.5
        _DepthColor("DepthColor", Color) = (1,0,0,1)
        _MainTex_C ("MainTex_C", 2D) = "white" {}
        _LoadingSpeed ("Loading Speed", Float ) = 0.2
        _LoadingLevel ("Loading Level", Float ) = 16
        _LoadingSize ("Loading Size", Range(0, 1)) = 0.5
        _LoadingL ("Loading Length", Range(0, 1)) = 0.5
        _LoadingW ("Loading Wdith", Range(0, 1)) = 0.3010045
        [MaterialToggle] _ScanlineDirection_UorV ("ScanlineDirection_UorV", Float ) = 0
        _ScanlineOpacity ("ScanlineOpacity", Float ) = 0.05
     

    }
    
    SubShader
    {

        Tags { "IgnoreProjector"="True"
            "Queue"="Transparent"
            "RenderType"="Transparent" }
        LOD 100    
        Blend SrcAlpha OneMinusSrcAlpha
        ZWrite Off 
 
        Pass
        {
            CGPROGRAM
            #pragma vertex vert
            #pragma fragment frag
            
            #include "UnityCG.cginc"
            #include "../ShaderLibs/UIFX.cginc"
            #include "../ShaderLibs/UVOperator.cginc"
            struct appdata
            {
            	float4 vertex : POSITION;
            	float2 uv : TEXCOORD0;
            	float3 normal : NORMAL;
            	float4 tangent : TANGENT;
               
            };
            
            struct v2f
            {
                
                float2 uv : TEXCOORD0;
                float4 pos : SV_POSITION;
                
                float4 posWorld : TEXCOORD1;
                float3 normalDir : TEXCOORD2;
                float3 tangentDir : TEXCOORD3;
                float3 bitangentDir : TEXCOORD4;
            };

             float4 _Color;
             float _LoadingSpeed;
             float _Emission;
             float _FadeIn;
             float _FadeOut;
             float _RotateSpeed_R;
             float _RotateSpeed_G;
             float _RotateSpeed_B;
             float _DepthOffsetSpeed;
             sampler2D _MainTex_B; 
             float4 _MainTex_B_ST;
             float _ShinnySpeed;
             float _MainTexB_Bdepth;
             sampler2D _MainTex_C; 
             float4 _MainTex_C_ST;
             float _LoadingLevel;
             float _LoadingSize;
             float _LoadingL;
             float _LoadingW;
             fixed _ScanlineDirection_UorV;
             float _ScanlineOpacity;
             float4 _LoadingColor;
             float4 _DepthColor;

            v2f vert ( appdata v )
            {
                
                v2f o;
                o.uv = v.uv;
                o.normalDir = UnityObjectToWorldNormal(v.normal);
                o.tangentDir = normalize( mul( unity_ObjectToWorld, float4( v.tangent.xyz, 0.0 ) ).xyz );
                o.bitangentDir = normalize(cross(o.normalDir, o.tangentDir) * v.tangent.w);
                o.posWorld = mul(unity_ObjectToWorld, v.vertex);
                o.pos = UnityObjectToClipPos( v.vertex );
                return o;
            }
            
            fixed4 frag (v2f i ) : SV_Target
            {
                
               
                
                //计算法线 切线 和 视角方向
                i.normalDir = normalize(i.normalDir);
                float3x3 tangentTransform = float3x3( i.tangentDir, i.bitangentDir, i.normalDir);
                float3 viewDirection = normalize(_WorldSpaceCameraPos.xyz - i.posWorld.xyz);
                
                //计算uv
                float2 uv = i.uv;
                //float2 scaleUV= ScaleUV(uv,_FadeOut);//缩放消失
                
                
                //圆形进度条
                float2 loadingUV = ScaleUV(uv,_LoadingSize);//进度条UV
                float2 polarUV = loadingUV*2-1;//极坐标uv
                float polar = DrawPolar(polarUV);//极坐标
                float polarLayer = floor((1 - polar) * _LoadingLevel) / (_LoadingLevel - 1);//极坐标分层
                float loadingSpeed = frac(_Time.y*_LoadingSpeed);//进度读取速度
                float circle = length(loadingUV*3-1.5);//画圆
                float cdFlow01 =step(0.5,polarLayer+(loadingSpeed*2.2-0.55));//极坐标环绕01
                float cdFlow02 =step(0.5,(1 - (polarLayer+(loadingSpeed*2.2-1.65))));//极坐标环绕02
                float loadingLength =floor(_LoadingL+circle)*(1 - floor(circle));//计算长度
                float loadingWidth = step(_LoadingW,frac((polar*_LoadingLevel)));//计算宽度
                float loadingCircle = cdFlow01*cdFlow02*saturate(loadingLength*loadingWidth)*saturate(_FadeIn*-2+1.0);
                
                
                //RG通道的旋转效果
                float2 center = float2(0.5,0.5);//中心
                float2 fadeinUV = ScaleUV(uv,1-_FadeIn);//缩放
                float rotSpeedR = _Time.y* _RotateSpeed_R;//r通道的速度
                float rotSpeedG = _Time.y* _RotateSpeed_G;//g通道的速度
                float2 rotationR = Rotate2D(fadeinUV,center,rotSpeedR);//旋转r通道
                float2 rotationG = Rotate2D(fadeinUV,center,rotSpeedG);//旋转g通道
                float channelR = tex2D(_MainTex_B,rotationR).r;//采样r通道贴图
                float channelG = tex2D(_MainTex_B,rotationG).g;//采样g通道贴图
                
                //B通道的旋转和面向视角的位移
                float rotSpeedB =_RotateSpeed_B*_Time.y;//b通道速度                
                float depthB =(1 - (_MainTexB_Bdepth+sin(_Time.y*_DepthOffsetSpeed)*2))*0.05;
                float3 tangentPos = mul(tangentTransform, viewDirection);
                float2 tangentUV = depthB*tangentPos + uv;
                tangentUV = (_FadeIn*0.5 -tangentUV)/(_FadeIn*0.5 *2-1);
                float2 rotationB = Rotate2D(tangentUV,center,rotSpeedB);                
                //采样b通道贴图
                float channelB = tex2D(_MainTex_B,rotationB).b;
                float3 colorB =_DepthColor.rgb*channelB;
                
                float3 centerColor =lerp(_LoadingColor,_DepthColor,channelB);
                float3 finalColor = lerp(_Color.rgb,centerColor,loadingCircle+channelB)*_Emission;
                
                float2 shinnySpeed = float2(_Time.x*_ShinnySpeed,_Time.x*_ShinnySpeed);
                float flicker = Flick(shinnySpeed);
               
                //采样背景贴图
                float4 backgroundTex = tex2D(_MainTex_C,uv);                
                float frame = backgroundTex.g;//边框
                float favoseShape = backgroundTex.b;//蜂窝纹理
                float bgMask = backgroundTex.a;//遮罩                                                                
                
                float2 favoseUV = float2(favoseShape, uv.y)+float2(0.1,0)*_Time.y;
                float4 favoseTex = tex2D(_MainTex_C,favoseUV);
                
                float honeycomb =favoseTex.r* favoseShape*2;
                
                
                float bgflicker =flicker *bgMask*0.02;
                float finalPattern = loadingCircle+channelR+channelG+channelB;
                float2 scanlineUV = ((uv+_Time.y*0.02)*128);
                float scanLineUorV = lerp( scanlineUV.x, scanlineUV.y, _ScanlineDirection_UorV );
                
                float scanLines = saturate(abs(sin(scanLineUorV)*_ScanlineOpacity));
                
                float bgPattern =(frame+scanLines+honeycomb)*bgMask;
                
                float initiate;
                
                FadeoutGlitch(_FadeOut,flicker,initiate);

                return fixed4(finalColor,(finalPattern+bgPattern+bgflicker)*initiate);
            






              
                
            }
            ENDCG
        }
    }
    
    
    
}
